package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.POJO.OrderSetting;
import com.itheima.dao.OrderSettingDao;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;


    public void add(List<OrderSetting> orderSettingList) {
        //判断传来的数据
        if(orderSettingList != null && orderSettingList.size() >0){
            //遍历
            for (OrderSetting orderSetting : orderSettingList) {
                //查询当前预约信息是否已经存在
                long count = orderSettingDao.countOrderSetting(orderSetting.getOrderDate());
                if(count >0){//已经存在，执行更新操作
                    orderSettingDao.updateNumberByOrderDate(orderSetting);
                }else{
                    //还未存在，直接进行添加
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    public List<Map> getOrderSettingByMonth(String date) {
        //建立查询范围
        String beginDay = date + "-1";
        String endDay = date + "-31";

        Map<String,String> map = new HashMap<>();
        map.put("beginDay",beginDay);
        map.put("endDay",endDay);

        List<OrderSetting> orderSettingList = orderSettingDao.getOrderSettingByMonth(map);
        List<Map> data = new ArrayList<>();
        if(orderSettingList != null && orderSettingList.size() >0){
            //创建一个集合，用户保护将要传输到页面的数据
            for (OrderSetting orderSetting : orderSettingList) {
                Map orderSettingMap = new HashMap<>();
                Date orderDate = orderSetting.getOrderDate();
                int number = orderSetting.getNumber();
                int reservations = orderSetting.getReservations();
                orderSettingMap.put("date",orderDate.getDate());
                orderSettingMap.put("number",number);
                orderSettingMap.put("reservations",reservations);
                data.add(orderSettingMap);
            }
        }
        return data;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //查询当前预约信息是否已经存在
        long count = orderSettingDao.countOrderSetting(orderSetting.getOrderDate());
        if(count >0){//已经存在，执行更新操作
            orderSettingDao.updateNumberByOrderDate(orderSetting);
        }else{
            //还未存在，直接进行添加
            orderSettingDao.add(orderSetting);
        }
    }
}
